#include "TopMessage.h"



TopMessage::TopMessage( MessageType _type, BYTE* _buffer /*= NULL*/ )
	:PipeMessage(_type)
{
	ZeroMemory(mData, sizeof(mData));

	if(_buffer)
	{
		mBuffer = _buffer;
		ParseBuffer();
	}
}




void TopMessage::SetPlayer( string _username, ULONG _totalPoints, UINT _pos, UINT _bestScore /*= 0*/, USHORT _numberPlayedGames /*= 0 */ )
{
	if( _pos > 14)
		return;

	if( _username.size() > 15)
		strcpy_s(mData[_pos].username, 15, _username.substr(0, 15).c_str());
	else
		strcpy_s(mData[_pos].username, 15, _username.c_str());

	mData[_pos].isSet = true;
	mData[_pos].totalPoints = _totalPoints;
	mData[_pos].bestScore = _bestScore;
	mData[_pos].numberPlayedGames = _numberPlayedGames;


	mBufferChanged = true;
}

UINT TopMessage::GetBufferSize()
{
	return sizeof(mData);
}

UINT TopMessage::CreateBuffer( UINT _startPos /*= 0*/ )
{
	memcpy_s(mBuffer + _startPos, sizeof(mData), &mData, sizeof(mData));
	_startPos +=  sizeof(mData);

	return _startPos;
}

void TopMessage::ParseBuffer()
{
	memcpy_s(&mData, sizeof(mData), mBuffer + sizeof(MessageType),  sizeof(mData));
}
